package com.example.xutils;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class MainActivity extends Activity {

	// 注解方式与控件关联
	@ViewInject(R.id.textView_main)
	private Button button_main;
	@ViewInject(R.id.imageView_main)
	private ImageView imageView_main;
	@ViewInject(R.id.listView_main)
	private ListView listView_main;

	@SuppressWarnings("unused")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 注解关联Activity,这一句必须有
		ViewUtils.inject(this);

		HttpUtils httpUtils = new HttpUtils();
		/**
		 * HttpUtils的GET方式请求
		 */
		httpUtils.send(HttpMethod.GET, "http://www.baidu.com/img/bd_logo1.png",
				new RequestCallBack<String>() {

					// step1
					@Override
					public void onStart() {
						Log.i("GET", "开始--->");
					}

					// step2
					@Override
					public void onLoading(long total, long current,
							boolean isUploading) {
						Log.i("GET", "链接中--->");
					}

					// 链接不成功在step1后调用
					@Override
					public void onFailure(HttpException error, String msg) {
						Log.i("GET", error.getExceptionCode() + ":" + msg);
					}

					// step3
					public void onSuccess(ResponseInfo<String> result) {
						Log.i("GET", "链接成功--->" + result.result);
					}
				});

		/**
		 * HttpUtils的POST方式请求
		 */
		RequestParams params = new RequestParams();
		params.addBodyParameter("aid", "2315431");
		params.addBodyParameter("msg", "挺好的文章");
		params.addBodyParameter("username", "匿名");

		httpUtils.send(HttpMethod.POST,
				"http://www.3dmgame.com/sitemap/api.php?type=2", params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						Log.i("POST", "开始链接+++>");
					}

					@Override
					public void onLoading(long total, long current,
							boolean isUploading) {
						Log.i("POST", "链接中+++>");
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Log.i("POST", error.getExceptionCode() + ":" + msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> result) {
						Log.i("POST", "链接成功+++>" + result.result);
					}
				});

		/**
		 * HttpUtils的下载文件
		 */
		// 支持断点续传，随时停止下载任务，开始任务
		HttpHandler<File> httpHandler = httpUtils.download(
				"http://www.baidu.com/img/baidu_jgylogo3.gif",
				"/mnt/sdcard/Download/baidu_jgylogo3.gif", true, // 如果目标文件存在，接着未完成的部分继续下载。服务器不支持RANGE时将从新下载
				true, // 如果从请求返回信息中获取到文件名，下载完成后自动重命名
				new RequestCallBack<File>() {

					@Override
					public void onStart() {
						Log.i("download", "开始下载===>");
					}

					@Override
					public void onLoading(long total, long current,
							boolean isUploading) {
						Log.i("download", "下载中===>");
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Log.i("download", "下载失败===>" + error.getExceptionCode());
					}

					@Override
					public void onSuccess(ResponseInfo<File> result) {
						Log.i("download", "下载成功===>" + result.result.getPath());
					}
				});
		// 调用cancel()方法停止下载
		httpHandler.cancel();

		/**
		 * HttpUtils上传文件,POST方式
		 */

		/**
		 * BitmapUtils加载图片
		 */
		BitmapUtils bitmapUtils = new BitmapUtils(this);
		// 加载网络图片
		bitmapUtils.display(imageView_main,
				"http://www.baidu.com/img/bd_logo1.png");
		// 加载本地图片(路径以/开头， 绝对路径)
		bitmapUtils
				.display(imageView_main, "/mnt/sdcard/Download/bd_login.png");
		// 加载assets中的图片(路径以assets开头)
		bitmapUtils.display(imageView_main, "assets/img/ic_launcher.png");

		// 使用ListView等容器展示图片时可通过PauseOnScrollListener控制滑动和快速滑动过程中时候暂停加载图片
		listView_main.setOnScrollListener(new PauseOnScrollListener(
				bitmapUtils, false, true));
		// listView_main.setOnScrollListener(new
		// PauseOnScrollListener(bitmapUtils, false, true, customListener));

	}

	/**
	 * 注解监听事件,监听方法名是自定义的，@OnClick({,,,})
	 * 
	 * @param v
	 */
	// 但是一定要保证方法的访问修饰符为public，
	// 同时方法的参数要与Android原来的监听方法参数一致，不仅参数类型，而且要保证参数的顺序
	@OnClick(R.id.textView_main)
	public void buttonClick(View v) {// 方法名称随意定义
		Toast.makeText(this, "haha", Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
